var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

// 数据库配置
let modle = require("../modle/db")


// 配置接口

router.get('/index',async (req, res) => {
  let sql = `select * from student`
  let [err, data] = await modle.query(sql)
  if (err) {
    res.send({ "code": "400", "msg": "查询失败" })
  } else { 
    res.send({ "code": "200", "msg": "查询成功",data:data})

  }
})


// 删除功能

router.get("/del", (req,res) =>{ 
  // console.log(req.query.id);
  let sql = `delete from student where id=${req.query.id}`
  modle.query(sql, (err) => {
    if (err) {
      res.send("查询失败")
    } else { 
      res.redirect("/del")
      
    }
  })
})

// 修改功能 

router.get("/xiugai", async (req,res) =>{ 
  // console.log(req.query.id);
  let sql = `select 姓名,性别,年龄 from student where id=${req.query.id}`
  let [err, data] = await modle.query(sql)
  res.send({data:data})
})


module.exports = router;
